package com.dkd.flow.strategy.service.operation;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSONObject;
import com.dkd.common.core.domain.entity.SysUser;
import com.dkd.flow.context.ProcessContext;
import com.dkd.flow.context.ProcessContextBuilder;
import com.dkd.flow.domain.EProcessInstances;
import com.dkd.flow.domain.EProcessTask;
import com.dkd.flow.emums.FlowOperation;
import com.dkd.flow.emums.FlowStatus;
import com.dkd.flow.emums.TaskActionType;
import com.dkd.flow.emums.TaskStatus;
import com.dkd.flow.strategy.annotation.Strategy;
import com.dkd.flow.utils.TaskCreate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
@Strategy(FlowOperation.SAVE)
public class SaveStrategy extends BaseStrategy {
    @Autowired
    ProcessContextBuilder processContextBuilder;
    @Autowired
    TaskCreate taskCreate;
    @Override
    public Long execute(JSONObject data) {
        // 使用构建器初始化上下文 --- start --- //
        ProcessContext context = processContextBuilder.build(data);
        EProcessInstances eProcessInstances = context.getEProcessInstances();
        EProcessTask curTask = context.getCurTask();
        Long curTaskStatus = context.getCurTaskStatus();
        JSONObject formValue = context.getFormValue();
        Long processDefinitionId = context.getProcessDefinitionId();
        // 使用构建器初始化上下文 --- end --- //
        if (curTaskStatus == null) {
            eProcessInstances = createEProcessInstances(eProcessInstances,
                    FlowStatus.DRAFT,
                    null);
            Long taskGroupId = taskCreate.createAndSetOperationGroupId(eProcessInstances);
            initNodeRelations(processDefinitionId);
            //创建发起人重新发起的任务
            JSONObject launchNode = links.get(0)
                    .getJSONObject("toNode");
            if (launchNode == null) {
                throw new RuntimeException("links 中第一个节点的 toNode 为 null，无法创建发起人任务");
            }
            SysUser liableUser = nodeUtils.getLiableUser(eProcessInstances);
            //创建	发起人任务 INITIATOR_TASK 1	自己	草稿 DRAFT 11	草稿 DRAFT 0
            Long launchTaskId = IdUtil.getSnowflakeNextId();
            curTask = taskCreate.createLaunchTask(eProcessInstances,
                    launchNode,
                    null,
                    TaskStatus.DRAFT,
                    launchTaskId,
                    null,
                    null,
                    liableUser,
                    taskGroupId,
                    JSONObject.toJSONString(formValue),
                    TaskActionType.LOG_TASK);
        } else {
            eProcessInstances = context.getEProcessInstances();
            eProcessInstances.setFormValue(JSONObject.toJSONString(context.getFormValue()));
            updateEProcessInstances(eProcessInstances);
            //创建	审批人任务 APPROVER_TASK 2	自己	草稿 DRAFT 11	草稿 DRAFT 0
            Long approverTaskId = IdUtil.getSnowflakeNextId();
            taskCreate.approverTask(eProcessInstances,
                    nodeUtils.getApproverByTask(curTask),
                    curTask.getProcessDesignId(),
                    TaskStatus.DRAFT,
                    approverTaskId,
                    null,
                    null,
                    curTask.getTaskGroupId(),
                    JSONObject.toJSONString(formValue),
                    TaskActionType.LOG_TASK);
        }
        return curTask.getID();
    }
}